package com.tcu.part.controller;

import com.tcu.exception.SmartException;
import com.tcu.part.entity.PartNum;
import com.tcu.part.service.IPartNumService;
import com.tcu.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/partNum")
public class PartNumController {
    private IPartNumService partNumService;

    @Autowired
    public void setPartNumService(IPartNumService partNumService) {
        this.partNumService = partNumService;
    }

    @PostMapping("add")
    public R addPartNumItem(@RequestBody PartNum partNum){
        if(partNum.getPartId()==null){
            throw new SmartException(20001,"要增加的零件数目项的id为空");
        }
        boolean save = partNumService.save(partNum);
        return save ? R.success() : R.error();
    }
    @DeleteMapping("{id}")
    public R removeNumItem(@PathVariable String id){
        boolean b = partNumService.removeById(id);
        return b ? R.success() : R.error();
    }
    @PostMapping("update")
    public R updateNumItem(@RequestBody PartNum partNum){
        if(partNum.getPartId()==null){
            throw new SmartException(20001,"没有id，更新失败");
        }
        boolean b = partNumService.updateById(partNum);
        return b ? R.success() : R.error();
    }
    @GetMapping("getOne/{id}")
    public R getById(@PathVariable String id){
        PartNum byId = partNumService.getById(id);
        return R.success().data("partNumInfo", byId);
    }
    @GetMapping("list")
    public R list(){
        List<PartNum> list = partNumService.list();
        return R.success().data("list",list);
    }
}
